import os
import tarfile

# 创建一个tar文件
with tarfile.open('example.tar', 'w') as tar:
    tar.add('../file1.txt')
    tar.add("../file2.txt")

# 读取tar文件
with tarfile.open('example.tar', 'r') as tar:
    file_numbers = tar.getmembers()
    print("tar file: ", file_numbers)
    '''[<TarInfo '../file1.txt' at 0x7fcb1a3c9e40>, <TarInfo '../file2.txt' at 0x7fcb1a3cab00>]'''

    tar.extractall('dir1/test')
